<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="../js/axios.js"></script>
    <script>
        // ES7才有async/await函数,返回的也是promise实例对象
        // 1.基本用法
        async function queryData() {
            // await后面必须跟随promise的实例对象
            var ret = await axios.get('http://localhost:3000/data');
            console.log(ret);
            return ret.data;
        };
        queryData().then(function(data) {
            console.log(data);
        });



        async function abc() {
            var res = await new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve('nihao')
                }, 3000)
            })
            return res;
        }
        queryData().then(function(data) {
            console.log(data);
        })

        // 2.多个异步请求
        async function queryData1() {
            var info = await axios.get('http://localhost:3000/data');
            var ret = await axios.get('http://localhost:3000/data' + info.data);
            return ret.data;
        }
        queryData1().then(function(data) {
            console.log(data);
        })
    </script>
</body>

</html>